<?php
class system_user_controller_panel extends system_controller_action {
	
	public static $_keyword = 'Keywords...';
	protected $user = null;
	protected $referer = null;
	
	public function init($args = array()) {
		if($this->getRequest()->getControllerName() != 'index') {
			if($this->admin($args)) {
				return false;
			}
		}
		$this->layout()->setLayout('layout');
		$this->view->user = $this->user = system_user_instance::getRow();
		if($this->view->referer === null) {
			$this->view->referer = $this->getRequest()->getHeader('referer');
		}
		$this->referer = $this->view->referer;
		$language = ($this->user) ? $this->user->getLang() : system_user_instance::getInstance()->getSession()->getLanguage();
		system_nav_language::getInstance()->setLanguage($language);
		$langSetting = new system_setting_language();
		$this->_crumbs()->addPage(system_nav_page::create($langSetting->getTitle($language))->setRoute('index/index'));
		return true;
	}
	
	protected function _saveToDatabase($row, $upload, $api, $rigorous = true, $saveParams = null) {
		try {
			system_db::getAdapter()->beginTransaction();
			if($this->_post('delete')) {
				$this->_auth();
				$row->delete();
			} else {
				if($this->_post('deleteResource')) {
					$row->set($upload, '');
				} else {
					if(isset($_FILES[$upload]) && is_uploaded_file($_FILES[$upload]['tmp_name'])) {
						$class = 'resource_api_' . strtolower($api);
						$rigorous = $upload == 'cover' ? false : $rigorous;
						$_api = new $class();
						if(1 && $watermark = $this->_post('watermark')) {
							$_api->setWatermark($watermark);
						}
						$res = $_api->create($_FILES[$upload], $rigorous);
						if($res) {
							$row->set($upload, $res->getHash());
						}
						$res->save();
					}
				}
				$row = $row->save($saveParams);
			}
			system_db::getAdapter()->commit();
			$this->view->throwMessage = 'Complete';
		} catch (Exception $e) {
			system_db::getAdapter()->rollBack();
			$this->view->throwException = $e;
		}
		return $row;
	}
	
	protected function _batchDelete(system_concept_abstract $concept, $name, $whereName) {
		$checkedList = array();
		if($this->getRequest()->isPost() && $this->_post('delete')) {
			$checkedList = (array) $this->_post($name);
			try {
				system_db::getAdapter()->beginTransaction();
				if(empty($checkedList)) {
					throw new picture_exception("No Item selected.");
				}
				$this->_auth();
				foreach($checkedList AS $id) {
					$_id = is_numeric($id) || !strpos($id, "'") ? "'$id'" : $id;
					$concept->delete($concept->fetch("$whereName = $_id"));
				}
				system_db::getAdapter()->commit();
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				system_db::getAdapter()->rollBack();
				$this->view->throwException = $e;
			}
		}
		$this->view->checkedList = $checkedList;
	}
	
	/**
	 *
	 * @return system_nav_crumbs
	 */
	protected function _crumbs() {
		return system_nav_crumbs::getInstance();
	}
	
	protected function language($name) {
		return system_nav_language::getInstance()->get($name);
	}
	
	protected function _go($referer = null, $router = 'index/index') {
		if($referer === null) {
			if($this->referer) {
				$this->getResponse()->setRedirect($this->referer);
			} else {
				$this->_goRoute($router);
			}
		} else {
			$this->getResponse()->setRedirect($referer);
		}
	}
	
	protected function _auth() {
		if($this->user->getCredential() != md5($this->_post('password'))) {
			throw new picture_exception("Invalid Password.");
		}
	}
	
	protected function _goRoute($name, array $urlOptions = array(), $reset = false) {
		$this->redirector()->gotoRouteAndExit($name, $urlOptions, $reset);
	}
	
	protected function _post($name = null, $default = null) {
		return $this->getRequest()->getPost($name, $default);
	}
	
	protected function _get($name = null, $default = null) {
		return $this->getRequest()->getQuery($name, $default);
	}
	
	protected function _param($name, $default = null) {
		return $this->getRequest()->getParam($name, $default);
	}

}